package com.businessportal.web.controllers.monitoring;

import com.businessportal.web.dataaccess.entities.Login;
import com.businessportal.web.dataaccess.services.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Vadim on 7/28/14.
 *
 * Pingdom Account:
 * URL: https://my.pingdom.com
 * User: str.vadim@gmail.com
 * Password: bullshit128
 *
 */

@WebServlet(urlPatterns = "/publicpages/pingdom.do")
public class MonitorController  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String status = "DOWN";
        long startTime = System.nanoTime();

        try {
            Login user = UserService.INSTANCE.CheckLoginPass("login0", "Password");

            status = "OK";
        } catch(Exception e){
        }

        long endTime = System.nanoTime();
        PrintWriter out = resp.getWriter();
        StringBuffer sb=new StringBuffer();
        sb.append("<?xml version='1.0' encoding='ISO-8859-1'?>\n");
        sb.append("<pingdom_http_custom_check>");
        sb.append("<status>" + status + "</status>\n");
        sb.append("<response_time>" + (endTime - startTime) + "</response_time>\n");
        sb.append("</pingdom_http_custom_check>");
        out.println(sb.toString());
        out.flush();
    }
}
